package com.apache.bid.service.impl;

import java.util.List;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidPub;
import com.apache.bid.manager.BidPubManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidPubManagerImpl implements BidPubManager {

	protected IDao bidPubDao;
	protected final String entityName = "com.apache.bid.entity.BidPub";
	protected Map<String, PluginConnector> plugins;

	public void setPlugins(Map<String, PluginConnector> plugins) {
		this.plugins = plugins;
	}

	public void setBidPubDao(IDao bidPubDao) {
		this.bidPubDao = bidPubDao;
	}

	public String saveInfo(ParamsVo<BidPub> vo) throws BusinessException {
		BidPub bidPub = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidPub_"+infoId;
		bidPub.setPubId(infoId);
		if (Constant.getInstance().ifWriteDatabase()) {
			//设置缺省值
			bidPub.setItemId(Validator.getDefaultStr(bidPub.getItemId(), "0"));
			bidPub.setBailCurrency(Validator.getDefaultStr(bidPub.getBailCurrency(), "CNY"));
			bidPub.setBailPriceUnit(Validator.getDefaultStr(bidPub.getBailPriceUnit(), "元"));
			bidPub.setBailPayMode(Validator.getDefaultStr(bidPub.getBailPayMode(), "0"));
			bidPub.setBailPayModeDesc(Validator.getDefaultStr(bidPub.getBailPayModeDesc(), "默认在线支付方式"));
			bidPub.setFeeRate(Validator.getDefaultStr(bidPub.getFeeRate(), "0"));
			bidPub.setFeeFix(Validator.getDefaultStr(bidPub.getFeeFix(), "0"));
			bidPub.setFeeGroup(Validator.getDefaultStr(bidPub.getFeeGroup(), "0"));
			bidPub.setFeeValue(Validator.getDefaultStr(bidPub.getFeeValue(), "0"));
			bidPub.setFeeValueMin(Validator.getDefaultStr(bidPub.getFeeValueMin(), "0"));
			bidPub.setFeeValueMax(Validator.getDefaultStr(bidPub.getFeeValueMax(), "0"));
			bidPub.setIsBroadCast(Validator.getDefaultStr(bidPub.getIsBroadCast(), "F"));//是否现场直播
			bidPub.setRuleCtrl(Validator.getDefaultStr(bidPub.getRuleCtrl(), "F"));//是否强制阅读竞价规则
			bidPub.setAuditLevel(Validator.getDefaultStr(bidPub.getAuditLevel(), "1"));//竞买人级别（默认不需要审核）

			MethodParam param = new MethodParam("BidPub", cacheKey, "", entityName);
			param.setVaule(bidPub);
			if (bidPubDao.insert(param)) {
				return infoId;
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidPub> vo) throws BusinessException {
		BidPub bidPub = vo.getObj();
		if (Validator.isNotNull(bidPub.getPubId())) {
			String cacheKey = "";
			//String cacheKey="bidPub_"+bidPub.getPubId();
			if (Constant.getInstance().ifWriteDatabase()) {
				MethodParam param = new MethodParam("BidPub", cacheKey, "", entityName);
				param.setVaule(bidPub);
				boolean mark = bidPubDao.edit(param);
				if (mark) {
					param.setKey("ById");
					param.setInfoId(bidPub.getPubId());
					bidPub = (BidPub) bidPubDao.selectById(param);
					BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("pub");
					if (cache.checkCacheObject(cache.pub_prefix + bidPub.getSessionId())) {
						cache.createCacheObject(cache.pub_prefix + bidPub.getSessionId(), bidPub);
					} else if (cache.checkCacheObject(cache.pub_prefix + bidPub.getItemId())) {
						cache.createCacheObject(cache.pub_prefix + bidPub.getItemId(), bidPub);
					}
				}
				return mark;
			} else {
				///////////////需要处理缓存结果//////////////////
				vo.setParams("methodCode", "editPub");
				return true;
			}
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidPub> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidPub_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		if (Constant.getInstance().ifWriteDatabase()) {
			if (Validator.isNull(infoId)) {
				String sessionId = String.valueOf(vo.getParams("sessionId"));
				if (Validator.isNotNull(sessionId)) {
					MethodParam param = new MethodParam("BySessionId", cacheKey, "", entityName);
					param.setParams("sessionId", sessionId);
					param.setDelete(true);
					return bidPubDao.delete(param);
				}
				String itemId = String.valueOf(vo.getParams("itemId"));
				if (Validator.isNotNull(itemId)) {
					MethodParam param = new MethodParam("ByItemId", cacheKey, "", entityName);
					param.setParams("itemId", itemId);
					param.setDelete(true);
					return bidPubDao.delete(param);
				}
				return false;
			}
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			if ("false".equals(mark)) {//逻辑删除
				param.setInfoId(infoId);
				BidPub info = (BidPub) bidPubDao.selectById(param);
				if (Validator.isEmpty(info)) {
					return false;
				}
				param.setVaule(info);//此处需要先将状态值赋值为删除状态
				param.setKey("BidPub");
				return bidPubDao.edit(param);
			} else {
				param.setParams("pubId", infoId);
				param.setDelete(true);
				return bidPubDao.delete(param);
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			if ("false".equals(mark)) {//逻辑删除
			} else {
			}
			vo.setParams("methodCode", "deletePub");
			return true;
		}
	}

	public Object getInfoById(ParamsVo<BidPub> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidPub_"+infoId;
		if (Validator.isNull(infoId)) {
			BidPub pub = vo.getObj();
			if (Validator.isEmpty(pub))
				return null;
			MethodParam param = new MethodParam("ByObjId", cacheKey, "", entityName);
			if (Validator.isNotNull(pub.getSessionId())) {
				param.setParams("sessionId", pub.getSessionId());
			}
			if (Validator.isNotNull(pub.getItemId())) {
				param.setParams("itemId", pub.getItemId());
			}
			return bidPubDao.selectSingle(param);
		} else {
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			param.setInfoId(infoId);
			return bidPubDao.selectById(param);
		}
	}

	public Object execute(ParamsVo<BidPub> vo) {
		String key = vo.getMethodKey();
		try {

			if ("singlePubByAttr".equalsIgnoreCase(key)) {
				return this.getSinglePubByAttr(vo.getObj());
			}

			if (!Validator.isEmpty(plugins)) {
				if (!Validator.isEmpty(plugins.get(key))) {
					return plugins.get(key).execute(vo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;
	}

	private BidPub getSinglePubByAttr(BidPub bidPub) {

		MethodParam param = new MethodParam("ByBidPubAttr", "", "", entityName);
		if (Validator.isNotNull(bidPub.getPubId())) {
			param.setParams("pubId", bidPub.getPubId());
		}
		if (Validator.isNotNull(bidPub.getItemId()) && !"0".equals(bidPub.getItemId())) {
			param.setParams("itemId", bidPub.getItemId());
		}
		if (Validator.isNotNull(bidPub.getSessionId()) && !"0".equals(bidPub.getSessionId())) {
			param.setParams("sessionId", bidPub.getSessionId());
		}
		if (!Validator.isEmpty(param.getParams()) && param.getParams().size() > 0) {
			BidPub pub = (BidPub) bidPubDao.selectSingle(param);
			return pub;
		} else {
			return null;
		}
	}

	public Page getPageInfo(ParamsVo<BidPub> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = bidPubDao.pageSelect(param);
		return page;
	}

	public List<BidPub> getList(ParamsVo<BidPub> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return bidPubDao.select(param);
	}

	public long countInfo(ParamsVo<BidPub> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidPubDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidPub> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidPubDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidPub bidPub = vo.getObj();
		param.setResultCache(false);
		if (!Constant.getInstance().ifWriteDatabase()) {
			param.setResultCache(false);
		}
		if (Validator.isEmpty(bidPub))
			return param;
		if (Validator.isNotNull(bidPub.getSessionId())) {
			sb.append(" and sessionId = :sessionId");
			param.setParams("sessionId", bidPub.getSessionId());
		}
		if (Validator.isNotNull(bidPub.getItemId())) {
			sb.append(" and itemId = :itemId");
			param.setParams("itemId", bidPub.getItemId());
		}
		if (Validator.isNotNull(bidPub.getBailCurrency())) {
			sb.append(" and bailCurrency = :bailCurrency");
			param.setParams("bailCurrency", bidPub.getBailCurrency());
		}
		if (Validator.isNotNull(bidPub.getBailPriceUnit())) {
			sb.append(" and bailPriceUnit = :bailPriceUnit");
			param.setParams("bailPriceUnit", bidPub.getBailPriceUnit());
		}
		if (Validator.isNotNull(bidPub.getBailTimeStart())) {
			sb.append(" and bailTimeStart = :bailTimeStart");
			param.setParams("bailTimeStart", bidPub.getBailTimeStart());
		}
		if (Validator.isNotNull(bidPub.getBailTimeEnd())) {
			sb.append(" and bailTimeEnd = :bailTimeEnd");
			param.setParams("bailTimeEnd", bidPub.getBailTimeEnd());
		}
		if (Validator.isNotNull(bidPub.getBailPayMode())) {
			sb.append(" and bailPayMode = :bailPayMode");
			param.setParams("bailPayMode", bidPub.getBailPayMode());
		}
		if (Validator.isNotNull(bidPub.getBailPayModeDesc())) {
			sb.append(" and bailPayModeDesc = :bailPayModeDesc");
			param.setParams("bailPayModeDesc", bidPub.getBailPayModeDesc());
		}
		if (Validator.isNotNull(bidPub.getFeeRate())) {
			sb.append(" and feeRate = :feeRate");
			param.setParams("feeRate", bidPub.getFeeRate());
		}
		if (Validator.isNotNull(bidPub.getFeeFix())) {
			sb.append(" and feeFix = :feeFix");
			param.setParams("feeFix", bidPub.getFeeFix());
		}
		if (Validator.isNotNull(bidPub.getFeeGroup())) {
			sb.append(" and feeGroup = :feeGroup");
			param.setParams("feeGroup", bidPub.getFeeGroup());
		}
		if (Validator.isNotNull(bidPub.getFeeValue())) {
			sb.append(" and feeValue = :feeValue");
			param.setParams("feeValue", bidPub.getFeeValue());
		}
		if (Validator.isNotNull(bidPub.getFeeValueMin())) {
			sb.append(" and feeValueMin = :feeValueMin");
			param.setParams("feeValueMin", bidPub.getFeeValueMin());
		}
		if (Validator.isNotNull(bidPub.getFeeValueMax())) {
			sb.append(" and feeValueMax = :feeValueMax");
			param.setParams("feeValueMax", bidPub.getFeeValueMax());
		}
		if (Validator.isNotNull(bidPub.getJust())) {
			sb.append(" and just = :just");
			param.setParams("just", bidPub.getJust());
		}
		if (Validator.isNotNull(bidPub.getMvUrl())) {
			sb.append(" and mvUrl = :mvUrl");
			param.setParams("mvUrl", bidPub.getMvUrl());
		}
		if (Validator.isNotNull(bidPub.getZtUrl())) {
			sb.append(" and ztUrl = :ztUrl");
			param.setParams("ztUrl", bidPub.getZtUrl());
		}
		if (Validator.isNotNull(bidPub.getNoticeUrl())) {
			sb.append(" and noticeUrl = :noticeUrl");
			param.setParams("noticeUrl", bidPub.getNoticeUrl());
		}
		if (Validator.isNotNull(bidPub.getRuleUrl())) {
			sb.append(" and ruleUrl = :ruleUrl");
			param.setParams("ruleUrl", bidPub.getRuleUrl());
		}
		if (Validator.isNotNull(bidPub.getChargeName())) {
			sb.append(" and chargeName = :chargeName");
			param.setParams("chargeName", bidPub.getChargeName());
		}
		if (Validator.isNotNull(bidPub.getDeptName())) {
			sb.append(" and deptName = :deptName");
			param.setParams("deptName", bidPub.getDeptName());
		}
		if (Validator.isNotNull(bidPub.getFax())) {
			sb.append(" and fax = :fax");
			param.setParams("fax", bidPub.getFax());
		}
		if (Validator.isNotNull(bidPub.getEmail())) {
			sb.append(" and email = :email");
			param.setParams("email", bidPub.getEmail());
		}
		if (Validator.isNotNull(bidPub.getTel())) {
			sb.append(" and tel = :tel");
			param.setParams("tel", bidPub.getTel());
		}
		if (Validator.isNotNull(bidPub.getAddress())) {
			sb.append(" and address = :address");
			param.setParams("address", bidPub.getAddress());
		}
		if (Validator.isNotNull(bidPub.getMemberName())) {
			sb.append(" and memberName = :memberName");
			param.setParams("memberName", bidPub.getMemberName());
		}
		if (Validator.isNotNull(bidPub.getMemberTel())) {
			sb.append(" and memberTel = :memberTel");
			param.setParams("memberTel", bidPub.getMemberTel());
		}
		if (Validator.isNotNull(bidPub.getSellerName())) {
			sb.append(" and sellerName = :sellerName");
			param.setParams("sellerName", bidPub.getSellerName());
		}
		if (Validator.isNotNull(bidPub.getIsBroadCast())) {
			sb.append(" and isBroadCast = :isBroadCast");
			param.setParams("isBroadCast", bidPub.getIsBroadCast());
		}
		if (Validator.isNotNull(bidPub.getRuleCtrl())) {
			sb.append(" and ruleCtrl = :ruleCtrl");
			param.setParams("ruleCtrl", bidPub.getRuleCtrl());
		}
		if (Validator.isNotNull(bidPub.getInfoDesc())) {
			sb.append(" and infoDesc = :infoDesc");
			param.setParams("infoDesc", bidPub.getInfoDesc());
		}
		if (Validator.isNotNull(bidPub.getRulemask())) {
			sb.append(" and rulemask = :rulemask");
			param.setParams("rulemask", bidPub.getRulemask());
		}
		if (Validator.isNotNull(bidPub.getAgentOrg())) {
			sb.append(" and agentOrg = :agentOrg");
			param.setParams("agentOrg", bidPub.getAgentOrg());
		}
		if (Validator.isNotNull(bidPub.getCreateUserId())) {
			sb.append(" and createUserId = :createUserId");
			param.setParams("createUserId", bidPub.getCreateUserId());
		}
		if (Validator.isNotNull(bidPub.getCreateUserName())) {
			sb.append(" and createUserName = :createUserName");
			param.setParams("createUserName", bidPub.getCreateUserName());
		}
		if (Validator.isNotNull(bidPub.getBjServer())) {
			sb.append(" and bjServer = :bjServer");
			param.setParams("bjServer", bidPub.getBjServer());
		}
		if (Validator.isNotNull(bidPub.getBjPort())) {
			sb.append(" and bjPort = :bjPort");
			param.setParams("bjPort", bidPub.getBjPort());
		}
		if (Validator.isNotNull(bidPub.getAuditLevel())) {
			sb.append(" and auditLevel = :auditLevel");
			param.setParams("auditLevel", bidPub.getAuditLevel());
		}
		if (Validator.isNotNull(bidPub.getIfPassword())) {
			sb.append(" and ifPassword = :ifPassword");
			param.setParams("ifPassword", bidPub.getIfPassword());
		}
		if (Validator.isNotNull(bidPub.getIfUpload())) {
			sb.append(" and ifUpload = :ifUpload");
			param.setParams("ifUpload", bidPub.getIfUpload());
		}
		if (Validator.isNotNull(bidPub.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidPub.getSpare1());
		}
		if (Validator.isNotNull(bidPub.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidPub.getSpare2());
		}
		if (Validator.isNotNull(bidPub.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidPub.getSpare3());
		}
		if (Validator.isNotNull(bidPub.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidPub.getSpare4());
		}
		if (Validator.isNotNull(bidPub.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidPub.getSpare5());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
